# 创建游戏角色类 Role
# 游戏人物角色拥有名字(name)、武器(weapon)等属性
# 游戏人物具有攻击方法(def attack)
# 武器通过武器类实现(Weapon)  -----  组合关系
class Role:
    def __init__(self, name, weapon):
        self.name = name
        self.weapon = weapon #r1.weapon=w1 让r1的weapon属性指向w1对象
    def attack(self, target):  # self.weapon: w1
        print("name: %s, weapon: %s, attack: %s, 掉了 %s 血" % (self.name, self.weapon.wname, target, self.weapon.strength))

class Weapon:
    def __init__(self, wname, strength):
        self.wname = wname
        self.strength = strength
if __name__ == '__main__':
    w1 = Weapon("zbsm", 98)  # wname = "zbsm"  strength = 98
    # print(w1.wname, w1.strength)  # zbsm 98
    r1 = Role("zhangfei", w1)  # name="zhangfei"  weapon=w1
    r1.attack("张苞")







